package cn.qin.xhangapi.controller;

import cn.qin.xhangapi.util.qiniuyun.QiniuUtil;
import cn.qin.xhangapi.util.response.ResponseResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件控制器
 *
 * @author 多仔ヾ
 */
@RestController
@RequestMapping("/api/file")
public class FileController {
    
    @Resource
    QiniuUtil qiniuUtil;

    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file) throws IOException {
        // 调用Qiniu工具类实现文件上传，上传后返回文件URL结果
        String path = qiniuUtil.uploadFile(file.getInputStream());

        // 把这个结果放到data中
        Map<String, Object> data = new HashMap<>();
        data.put("path", path);

        return ResponseResult.success(data);
    }
    
}